/*
 * @(#)TemplateFilter.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * TemplateFilter
 * 
 * @author BIS 2012-12-24
 */
public abstract class TemplateFilter implements Filter
{

    /**
     * filterConfig
     */
    private FilterConfig filterConfig;

    /**
     * @return javax.servlet.FilterConfig
     */
    protected final FilterConfig getFilterConfig()
    {
        return filterConfig;
    }

    /**
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public final void init(FilterConfig filterConfig) throws ServletException
    {
        this.filterConfig = filterConfig;
        initParameter(filterConfig);
    }

    /**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {

        if (doPreProcessing(request, response))
        {
            chain.doFilter(request, response);
            doPostProcessing(request, response);
        }
    }

    /**
     * doPreProcessing
     * @Description:
     * @param request
     * @param response
     * @return
     * @throws IOException
     * @throws ServletException
     */
    public abstract boolean doPreProcessing(ServletRequest request, ServletResponse response) throws IOException, ServletException;

    /**
     * doPreProcessing
     * @Description:
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public abstract void doPostProcessing(ServletRequest request, ServletResponse response) throws IOException, ServletException;

    /**
     * initParameter
     * @Description:
     * @param filterConfig
     * @throws ServletException
     */
    public abstract void initParameter(FilterConfig filterConfig) throws ServletException;

    /**
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy()
    {
        filterConfig = null;
    }

}
